package com.wit.coursesystem.dbhelper;

import java.util.List;

import com.wit.coursesystem.dao.CourseDao;
import com.wit.coursesystem.dao.DaoSession;
import com.wit.coursesystem.entity.Course;

import android.content.Context;

public class CourseHelper {
	private CourseDao courseDao;
	private static Context mContext;
	private static CourseHelper instance;
	
	private CourseHelper() {}
	
	public static CourseHelper getInstance(Context context) {
		if(instance == null) {
			instance = new CourseHelper();
			if(mContext == null)
				mContext = context;
			DaoSession session = DbHelper.getDaoSession(mContext);
			instance.courseDao = session.getCourseDao();
		}
		
		return instance;
	}
	
	public long insert(Course course) {
		return courseDao.insert(course);
	}
	
	public Course selectByName(String courseName) {
		return courseDao.queryBuilder().where(CourseDao.Properties.CourseName.eq(courseName)).unique();
	}
	
	public List<Course> selectAll() {
		return courseDao.queryBuilder().list();
	}
	
	public boolean deleteByName(String courseName) {
		Course course = selectByName(courseName);
		if(course == null) 
			return false;
		else
			courseDao.delete(course);
		return true;
	}
}
